﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DataProvider;
using System.Data.SqlClient;

namespace BusinessLayer
{
    public class VatTu :XL_Bang
    {
        #region Cac thuoc tinh
        private string _maVTU;
        public string MaVTU
        {
            get { return _maVTU; }
            set { _maVTU = value; }
        }
        private string _tenVTU;
        public string TenVTU
        {
            get { return _tenVTU; }
            set { _tenVTU = value; }
        }
        private string _dvt;
        public string DonViTinh
        {
            get { return _dvt; }
            set { _dvt = value; }
        }
        private string _phanTram;
        public string PhanTram
        {
            get { return _phanTram; }
            set { _phanTram = value; }
        }
        private string _hinh;
        public string Hinh
        {
            get { return _hinh; }
            set { _hinh = value; }
        }

        #endregion
        #region cac phuong thuc
        public DataTable GetData()
        {
            StrSQL = "select * from VATTU";
            return GetDataTable();
        }

        public bool AddNew()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_VATTU";
                command.Parameters.AddWithValue("@MAVTU", _maVTU);
                command.Parameters.AddWithValue("@TENVTU", _tenVTU);
                command.Parameters.AddWithValue("@DVTINH", _dvt);
                command.Parameters.AddWithValue("@PHANTRAM", _phanTram);
                command.Parameters.AddWithValue("@HINH", _hinh);
                command.Parameters.AddWithValue("@TYPE", 0);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }
        public bool Edit()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_VATTU";
                command.Parameters.AddWithValue("@MAVTU", _maVTU);
                command.Parameters.AddWithValue("@TENVTU", _tenVTU);
                command.Parameters.AddWithValue("@DVTINH", _dvt);
                command.Parameters.AddWithValue("@PHANTRAM", _phanTram);
                command.Parameters.AddWithValue("@HINH", _hinh);
                command.Parameters.AddWithValue("@TYPE", 1);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }
        public bool Delete()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_VATTU";
                command.Parameters.AddWithValue("@MAVTU", _maVTU);
                command.Parameters.AddWithValue("@TENVTU", string.Empty);
                command.Parameters.AddWithValue("@DVTINH", string.Empty);
                command.Parameters.AddWithValue("@PHANTRAM", string.Empty);
                command.Parameters.AddWithValue("@HINH", string.Empty);
                command.Parameters.AddWithValue("@TYPE", 2);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }
        public DataTable DanhSachVatTuChuaTungDatHang()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "sp_DanhSachVatTuChuaTungDatHang";
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable table = new DataTable();
                adapter.Fill(table);
                return table;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                command.Dispose();
                close();
            }
        }
        public DataTable TinhHinhNhapXuatVatTu()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "sp_TinhHinhNhapXuatVatTu";
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable table = new DataTable();
                adapter.Fill(table);
                return table;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                command.Dispose();
                close();
            }
        }

        public DataTable VatTuCoSoLuongXuatNhieuNhat()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "sp_VatTuCoSoLuongXuatNhieuNhat";
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable table = new DataTable();
                adapter.Fill(table);
                return table;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                command.Dispose();
                close();
            }
        }
        #endregion
  
    }

}
